package ch14_反射与注解.注解;

/**
 * @author 挚爱之夕
 * @date 2021/8/29 - 08 - 29 - 17:09
 * @Description: ch14_反射与注解.注解
 * @Version: 1.0
 */

//复数类
class Complex {
    double real;
    double image;

    public Complex(double real, double image) {
        this.real = real;
        this.image = image;
    }
    /*下面的Override注解用以标记toString是重写父类Object的方法*/
    /*若toString的方法名或参数写错了，编译器会报语法错误*/
    /*若未指定Override注解，则即使写错了也不会报语法错误*/

    @Override
    public String toString() {
        String sign = image >= 0 ? "+" : "-";
        return real + sign + Math.abs(image) + "i";
    }
}
//测试类
public class OverrideAnnotationDemo {
    public static void main(String[] args) {
        Complex c1 = new Complex(5.2,3);
        Complex c2 = new Complex(2.7,-1.6);
        System.out.println("c1 = "+c1.toString());
        System.out.println("c2 = "+c2.toString());
    }
}
